import Chalk from 'chalk';

export default {
	Chalk,
	checkPort(server,port){
		console.log(`checking port:${port} whether available`);
		server.on('listening',()=>{
			console.log(`port:${Chalk.green(port)} is available`);
		});
		server.on('error',err=>{
			if (err) {
				if (err.code=='EADDRINUSE') {
					console.log(`port:${Chalk.red(port)} is unavailable,please set Config.rootPort other port.`);
					console.log(`code:${err.code}`);
				}else{
					console.log(err);
				};
			};
		});
	},
	getArgtype(arg){
		return Object.prototype.toString.call(arg).toLowerCase().match(/\s(\w+)/)[1];
	},
	getArrayRepeatInfo(arr){
		arr=this.getArgtype(arr)=='array'?arr:[...[arr]];
		let has={
			index:-1,
			name:null
		};
		for(let i=0;i<arr.length; i++){
			if (has.name==arr[i]) {
				return Object.assign(has,{
					index:i,
					name:arr[i]
				});
			};
		}
		return has;
	},
	toRegExp(str){
		str=this.getArgtype(str)=='string'?str:JSON.stringify(str);
		return str=='*'?(/.+/gi):new RegExp(str.replace(/[\$\?\.\/\-\*\\]/g,'\\$&'),'gi');
	}
}